package presentation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridBagLayoutInfo;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.FileNotFoundException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.RectangleInsets;

import control.CControl;

public class PBatteryOverview extends JPanel implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CControl cControl;
	private JPanel BatteryVoltPanel;
	private JPanel batteryPackOverview;
	private ChartPanel[] batteryVoltPanels = new ChartPanel[6];
	private PBatteryVoltage battery_volt_data;
	
	public PBatteryOverview(CControl cControl) 
	{
		this.setBackground(Color.white);
		this.setLayout(new GridBagLayout());
		this.cControl = cControl;
		
		battery_volt_data = new PBatteryVoltage(cControl);
		
		battery_volt_data.addObserver(this);
		
		/*
		 * Battery Pack overall overview image
		 * 
		 * 
		 * */

		
		/*
		 * Battery pack primary data
		 * */
		JPanel battery_params = new JPanel();
		battery_params.setLayout(new GridLayout(3, 3));
		
		createReg(cControl, "Total Battery Pack Voltage", "MBN_BMS_Voltage", battery_params);
		createReg(cControl, "Total Battery Pack Current", "MBN_BMS_Current", battery_params);
		createReg(cControl, "Total Battery Pack Temperature", "MBN_BMS_Temperature", battery_params);

		
		battery_params.setBackground(Color.white);
		
		JFreeChart socChart = new JFreeChart("SOC",new PBatteryJuiceDial());
		ChartPanel socPanel = new ChartPanel(socChart);
		socChart.setPadding(new RectangleInsets(0,25,20,25));
		socChart.setBackgroundPaint(Color.white);
		
		
		/*
		 * Battery Voltages setup
		 * 
		 * */
		BatteryVoltPanel = new JPanel();
		BatteryVoltPanel.setBackground(Color.white);
		BatteryVoltPanel.setBorder(new EmptyBorder(0, 10, 10, 10) );
		
		
		BatteryVoltPanel.setLayout(new GridLayout(2,3,10,10));
		int size = 16;
		int offset = 0;
		for (int i = 0; i < 6; i++) {
			if(i == 2 || i == 3)
			{
				size = 18;
			}
			else
			{
				size = 16;
			}
			batteryVoltPanels[i] = new ChartPanel(PChartFactory.createBarChart(String.format("Bank %d", i + 1), offset, size));
			BatteryVoltPanel.add(batteryVoltPanels[i]);
			
			offset += size;
		}
		
		GridBagConstraints c = new GridBagConstraints();
		PBatteryPackImage p = new PBatteryPackImage(cControl);
		
		p.setMinimumSize(  new Dimension(330,260));
		p.setPreferredSize(new Dimension(330,260));
		p.setMaximumSize(  new Dimension(330,260));
		// battery pack image
		
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.CENTER;
		c.gridheight = 1;
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		c.weighty = 0;
		
		this.add(p,c);
		
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 1;
		c.weightx = 0.5;
		
		this.add(battery_params);
		
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.EAST;
		c.gridx = 2;
		c.gridy = 0;
		c.weightx = 0;
		c.weighty = 0;
		socPanel.setMinimumSize(new Dimension(250,250));
		this.add(socPanel,c);
		
		c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		c.gridheight = 2;
		c.gridwidth = 3;
		
		c.gridx = 0;
		c.gridy = 1;
		
		c.weightx = 1;
		c.weighty = 1;
		
		this.add(this.BatteryVoltPanel,c);
		
		
		this.updateUI();
		
		battery_volt_data.test();
	}
	
	private void createReg(CControl cControl,String disp_name, String reg_name ,JPanel container)
	{
		
		container.add(new JLabel(disp_name));
		PRegisterValue v = new PRegisterValue("-",0);
		JLabel unit = new JLabel(cControl.getRegisterUnit(reg_name));
		cControl.addRegisterObserver(reg_name, v);
		container.add(v);
		container.add(unit);
		
	}

	@Override
	public void update(Observable o, Object arg) {
		int size = 16;
		int offset = 0;
		for (int i = 0; i < 6; i++) {
			if(i == 2 || i == 3)
			{
				size = 18;
			}
			else
			{
				size = 16;
			}
			PChartFactory.updateBarChart(batteryVoltPanels[i].getChart(),battery_volt_data.get_bank(i), size, offset);
			
			offset += size;
		}
		this.updateUI();
	}
	
}



